package org.comix.util;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @Creator paladin
 * @Created-date 2009-1-15
 */
public abstract class StringUtil {

	protected static final Log logger = LogFactory.getLog(StringUtil.class);

	public static String parse(Map<?, ?> maps) {
		StringBuffer buffer = new StringBuffer();

		for (Iterator<?> itr = maps.keySet().iterator(); itr.hasNext();) {
			Object key = itr.next();
			buffer.append("[" + key + ":" + maps.get(key) + "]");
		}
		return buffer.toString();
	}

	public static boolean isEmpty(String str) {
		if (str == null) {
			return true;
		} else {
			return str.equals("");
		}
	}

	public static boolean isPrefix(String str, String prefix) {
		if (str == null) {
			return false;
		} else {
			if (prefix == null) {
				return true;
			}
			return str.startsWith(prefix);
		}
	}

	public static String replace(String src, String oldStr, String newStr) {
		StringBuffer buffer = new StringBuffer(src);

		int index = 0;
		while ((index = buffer.indexOf(oldStr, index)) != -1) {
			buffer.replace(index, index + oldStr.length(), newStr);
			index = index + newStr.length();
		}
		return buffer.toString();
	}

	public static String remove(String src, String key) {
		return replace(src, key, "");
	}

	public static String removeSpace(String str) {
		if (str == null) {
			return null;
		}
		char[] chars = str.toCharArray();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			if ((chars[i] == '\n') || chars[i] == '\t' || chars[i] == '\r' || chars[i] == ' ') {
				continue;
			} else {
				buffer.append(chars[i]);
			}
		}
		return buffer.toString();

	}

}
